package vip.huhailong.thumbnailatorstudy.service.impl;

import jakarta.servlet.http.HttpServletResponse;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
import net.coobird.thumbnailator.name.Rename;
import org.springframework.stereotype.Service;
import vip.huhailong.thumbnailatorstudy.service.IImageProcessingService;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.stream.Stream;

/**
 * 图片处理接口实现类
 */
@Service
public class ImageProcessingServiceImpl implements IImageProcessingService {

    private static final String[]imageSuffixArray = {".jpg",".jpeg",".png"};

    /**
     * 生成指定文件夹下所有图片的缩略图，并指定图片大小且保持原始比例
     * @param dirPath   指定文件夹
     * @param widthPix  缩略图的宽度
     * @param heightPix 缩略图的高度
     * @param imageType 缩略图类型
     */
    @Override
    public void generateThumbnailImageForDir(String dirPath, Integer widthPix, Integer heightPix, String imageType) {
        File[] files = new File(dirPath).listFiles();
        if(files != null && files.length > 0){
            File[] array = Stream.of(files).filter(item -> {
                String suffix = item.getName().substring(item.getName().lastIndexOf("."));
                return Arrays.stream(imageSuffixArray).anyMatch(item2 -> item2.equalsIgnoreCase(suffix));
            }).toArray(File[]::new);
            try{
                if(array.length > 0){
                    Thumbnails.of(array).size(widthPix, heightPix).outputFormat(imageType).toFiles(Rename.PREFIX_DOT_THUMBNAIL);
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    public void addWatermark(String sourceImagePath, String watermarkImagePath, Integer widthPix, Integer heightPix,Integer watermarkWidthPix, Integer watermarkHeightPix, Float opacity, HttpServletResponse response) {
        try {
            BufferedImage bufferedImage = Thumbnails.of(new File(watermarkImagePath)).size(watermarkWidthPix, watermarkHeightPix).asBufferedImage();
            Thumbnails.of(new File(sourceImagePath)).size(widthPix,heightPix).watermark(Positions.BOTTOM_RIGHT,bufferedImage,opacity)
                    .toOutputStream(response.getOutputStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
  public void rotation(String filePath, Double deg, Integer widthPix, Integer heightPix, String imageType, HttpServletResponse response) {
      try {
          Thumbnails.of(new File(filePath)).size(widthPix,heightPix).rotate(deg).toOutputStream(response.getOutputStream());
      } catch (IOException e) {
          throw new RuntimeException(e);
      }
  }

    @Override
    public void getImage(String filePath, Integer widthPix, Integer heightPix, Boolean aspect, Double opacity, HttpServletResponse response) {
        try {
            Thumbnails.of(new File(filePath)).size(widthPix,heightPix).keepAspectRatio(aspect).outputQuality(opacity).toOutputStream(response.getOutputStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
